﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using yibo.Models;

namespace YBPlatform.Controllers
{
    public class MyArticlesController : Controller
    {
        //
        // GET: /MyArticles/

        public ActionResult ArticlesList()
        {
            ArtBlogOperate dbOperate = new ArtBlogOperate();
            var articleList = dbOperate.GetAllArticleByBlogId(1);   //暂时取BlogId为1
            ViewData["articleList"] = articleList;
            Session["articleList"] = articleList;
            return View();
        }

        public ActionResult EditArticle(int id = -1)
        {
            ViewData["articleIndex"] = id;
            return View();
        }

        [ValidateInput(false)]
        public ActionResult ShowArticle(int id = -1)
        {
            if (id >= 0)
            {
                ViewData["articleIndex"] = id;
            }
            else 
            {
                article myArticle = new article();
                myArticle.articleContent = Request.Form["TextArea1"];
                myArticle.title = Request.Form["Text1"];
                ViewData["myArticle"] = myArticle;
            }
            return View();
        }

        [ValidateInput(false)]
        public ActionResult InsertArticle(int id = -1)
        {
            ArtBlogOperate dbOperate = new ArtBlogOperate();

            article myArticle = new article();
            myArticle.articleContent = Request.Form["TextArea1"];
            myArticle.title = Request.Form["Text1"];
            myArticle.blogId = 1;

            dbOperate.ArticleInsert(myArticle);

            TempData["operation"] = "insertArticle";
            return RedirectToAction("ArticlesList");
        }
    }
}
